package com.vim.modules.web.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.model.response.PageRestfulResponse;
import com.vim.common.utils.PageRestfulResponseUtils;
import com.vim.common.annotation.ParentModuleInfo;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.CmsContent;
import com.vim.modules.web.service.CmsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@ParentModuleInfo(moduleCode = "cms", moduleName = "内容管理")
@Api(value = "文章管理", description = "获取文章列表和文章内容")
@RestController
@RequestMapping(value = "/api/cms/")
public class CmsApiController {

    @Resource
    private CmsContentService cmsContentService;

    @ApiOperation(value = "获取文章列表", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":[{\"contentId\":\"0ce2587ca7454fd586342cc76ce6322e\",\"contentTitle\":\"标题1\",\"content\":\"<p>12<br>121212121212</p>\",\"createTime\":1531574833000},{\"contentId\":\"1\",\"contentTitle\":\"标题1\",\"content\":\"<p>12<br>121212121212</p>\",\"createTime\":1531574833000}],\"totalCount\":6,\"totalPage\":3,\"currentPage\":1,\"pageSize\":2}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true),
                        @ApiImplicitParam(name = "limit", value = "每页显示数量", required = true)})
    @RequestMapping(value = "/contentList")
    public PageRestfulResponse contentList(@RequestParam(value = "page") Integer page, @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
        PageHelper.startPage(page,limit);
        List<CmsContent> list = cmsContentService.findList(new CmsContent());
        PageInfo<CmsContent> pageInfo = new PageInfo<>(list);

        return PageRestfulResponseUtils.handle(pageInfo);
    }
}
